﻿//
// eCRAM, a monthly activity report web application
// Copyright (C) 2010  Aptea
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.Linq;
using eCRAM.Model;

namespace eCRAM.Dao
{
    /// <summary>
    /// This is a line inside the CRAM.
    /// </summary>
    public static class CramLineRepository
    {


        public static IEnumerable<CramLine> GetAll()
        {
            using (var context = new eCramDataContext())
            {
                return context.CramLines.ToList() ;
            }
        }

        public static IEnumerable<CramLine> GetByCramId(Guid cramId)
        {
            using (var context = new eCramDataContext())
            {
                return context.CramLines.Where(cl => cl.CramId == cramId).ToList();
            }
        }

        public static IEnumerable<CramLine> GetByActivityId(Guid activityId)
        {
            using (var context = new eCramDataContext())
            {
                return context.CramLines.Where(cl => cl.ActivityId == activityId).ToList();
            }
        }

        public static void Create(CramLine cramLine)
        {
            using (var context = new eCramDataContext())
            {
                context.CramLines.InsertOnSubmit(cramLine);
                context.SubmitChanges();
            }
        }

        public static void InsertCramLines(IEnumerable<CramLine> cramLines)
        {
            foreach (var cramLine in cramLines)
            {
                Create(cramLine);
            }
        }

        public static void Delete(CramLine cramLine)
        {
            using (var context = new eCramDataContext())
            {
                CramLine originalCramLine = context.CramLines.FirstOrDefault(cl => cl.Id == cramLine.Id);
                if (originalCramLine != null)
                {

                    context.CramLines.DeleteOnSubmit(originalCramLine);
                    context.SubmitChanges();
                }
            }
        }

        public static void DeleteByCramId(Guid cramId)
        {
            using (var context = new eCramDataContext())
            {
                foreach (var originalCramLine in context.Cram_DailyActivities.Where(cl => cl.CramLine.CramId == cramId))
                {
                    context.Cram_DailyActivities.DeleteOnSubmit(originalCramLine);
                }
                foreach (var originalCramLine in context.CramLines.Where(cl => cl.CramId == cramId))
                {
                    context.CramLines.DeleteOnSubmit(originalCramLine);
                }
                context.SubmitChanges();
            }
        }



        /// <summary>
        /// Get CramLine
        /// </summary>
        /// <param name="cramlineId"></param>
        /// <returns></returns>
        public static CramLine GetById(Guid cramlineId)
        {
            using (var context = new eCramDataContext())
            {
                return (context.CramLines.FirstOrDefault(p => p.Id == cramlineId));
            }
        }


        public static void Update(CramLine cramLine)
        {
            using (var context = new eCramDataContext())
            {
                LinqTools.Update(cramLine, c => c.Id == cramLine.Id, context);
                context.SubmitChanges();
            }
        }
    }
}
